﻿@{
    Layout = "~/Views/Shared/_LayoutMain.cshtml";
}
@{
    List<ConsumeSys.Model.User> userList = new ConsumeSys.BLL.UserBLL().GetUserList();
    List<ConsumeSys.Model.ConsumeType> consumeTypeList = new ConsumeSys.BLL.ConsumeBLL().GetConsumeTypeList();
}
<div id="dg"></div>
<div id="where" style="padding: 5px 5px;">
    <a href="javascript:Add()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a>
    <a href="javascript:Remove()" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true">删除</a>
    消费名称：<input type="text" id="c_Name" value="" style="width: 150px" />&nbsp;
    结算状态：<select name="c_SettlementStatus">
        <option value="">--请选择--</option>
        <option value="1">未结算</option>
        <option value="2">已结算</option>
    </select>&nbsp;
    付款人：
    <select name="c_UserId">
        <option value="0">--请选择--</option>
        @{
            if (userList != null && userList.Count > 0)
            {
                foreach (ConsumeSys.Model.User item in userList)
                {
                    <option value="@item.id">@item.u_RealName</option>
                }
            }
        }
    </select>&nbsp;
    类型：
    <select name="c_CTypeId">
        <option value="0">--请选择--</option>
        @{
            if (consumeTypeList != null && consumeTypeList.Count > 0)
            {
                foreach (ConsumeSys.Model.ConsumeType item in consumeTypeList)
                {
                    <option value="@item.id">@item.ct_TypeName</option>
                }
            }
        }
    </select>
    <a href="javascript:Query()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
</div>
<script type="text/javascript">
    $(function () {       
        $('#dg').datagrid({
            url: '/Consume/GetConsumeList',
            title: null,
            idField: 'id',
            border: false,
            fit: true,
            singleSelect: true,
            method: 'post',
            toolbar: "#where",
            pagination: true,
            pageSize: 50,
            pageNumber: 1,
            pageList: [100, 50, 30, 20],
            columns: [[
                { field: 'id', title: '主键', checkbox: true },
                { field: 'c_Name', title: '消费名称' },
                { field: 'RealName', title: '付款人' },
                { field: 'TypeName', title: '消费类型名称' },
                //{ field: 'c_TotalPrice', title: '消费总金额',align:'right'},
                {
                    field: 'c_ActualPayment', title: '实际付款金额', align: 'right', formatter: function (value, row, index) {
                        if (value != "" && value != undefined && value != null) {
                            return parseFloat(value).toFixed(2);
                        } else {
                            return "";
                        }
                    }
                },
                { field: 'c_PersonTotalCount', title: '参与总人数', align: 'right' },
                { field: 'c_SettlementStatusTXT', title: '结算状态' },
                {
                    field: 'c_CTime', title: '消费时间', formatter: function (value, row, index) {
                        if (value != "" && value != undefined && value != null) {
                            return value.toString().replace("T", " ");
                        } else {
                            return "";
                        }
                    }
                },
                { field: 'c_InputPerson', title: '录入人' },
                {
                    field: 'c_InputTime', title: '录入时间', formatter: function (value, row, index) {
                        if (value != "" && value != undefined && value != null) {
                            return value.toString().replace("T", " ");
                        } else {
                            return "";
                        }
                    }
                },
                { field: 'c_Remark', title: '备注' }
            ]],
            rowStyler: function (index, row) {
                if (row.c_SettlementStatusTXT == "已结算") {
                    return "background-color:#E0FFFF;color:#ccc;";
                }
            },
            onLoadSuccess: function (data) {
                if (data.rows.length > 0) {
                    //循环判断操作为新增的不能选择
                    for (var i = 0; i < data.rows.length; i++) {
                        //根据isFinanceExamine让某些行不可选
                        if (data.rows[i].c_SettlementStatusTXT == "已结算") {
                            $("input[type='checkbox']")[i + 1].disabled = true;
                        }
                        if (data.rows[i].id == "") {                        
                            $("input[type='checkbox']")[i + 1].remove();
                        }
                    }
                }
            }
        });
        $('#dg').datagrid("options").view.onBeforeRender = function (target, rows) {
            var jh = 0;
            for (var i = 0; i < rows.length; i++) {
                jh += parseFloat(rows[i].c_ActualPayment);
            }
            var newRow = {
                id: '',
                c_Name: '',
                RealName: '',
                TypeName: '合计',
                c_ActualPayment:jh.toFixed(2),
                c_PersonTotalCount: '',
                c_SettlementStatusTXT: '',
                c_CTime: '',
                c_InputPerson: '',
                c_InputTime: '',
                c_Remark: '',
            }
            rows.push(newRow);
        };
    })
    function Query() {
        $('#dg').datagrid('reload', {
            c_Name: $("#c_Name").val(),
            c_SettlementStatus: $("[name=c_SettlementStatus] option:selected").val(),
            c_UserId: $("[name=c_UserId] option:selected").val(),
            c_CTypeId: $("[name=c_CTypeId] option:selected").val()
        });
    }
    function Add() {
        window.parent.addTab('添加消费记录', '/Consume/AEConsume');
    }
    function Remove() {
        $.messager.confirm("警告", "你真的要删除吗", function (r) {
            if (r) {
                var rows = $('#dg').datagrid('getSelections');
                var ids = new Array();
                for (var i = 0; i < rows.length; i++) {
                    ids.push(rows[i]["id"]);
                }
                if (ids.length == 0) {
                    $.messager.show({
                        title: '提示',
                        msg: '请选择你要删除的记录行',
                        timeout: 5000,
                        showType: 'slide'
                    });
                    return;
                }
                var _ids = ids.join(",");
                $.ajax({
                    type: "POST",
                    url: "/Consume/DeleteConsume",
                    data: { ids: _ids },
                    success: function (d) {
                        if (d.Code == 200) {
                        $('#dg').datagrid('reload'); 
                        $.messager.show({
                            title: '提示',
                            msg: '删除成功',
                            timeout: 2000,
                            showType: 'slide',
                            style: {
                                right: '',
                                top: 100,
                                bottom: ''
                            }
                        });                      
                    } else {
                        $.messager.alert('Warning',d.Msg);
                    }  
                        //if (msg == "success") {
                        //    S_success();
                        //} else if (msg == "error") {
                        //    E_error();
                        //} else {
                        //    E_errorMsg(msg);
                        //}
                    }
                });
            }
        })
    }
</script>

